package cn.bdqn.factory;

import cn.bdqn.anno.Component;

import java.util.HashMap;
import java.util.Map;

public class SpringFactory {
    //用来存放工厂创建的对象
    static Map<String,Object> map=new HashMap<>();

    static {
        try {
            //获取class对象
            Class clz= Class.forName("cn.bdqn.dao.UserDao");
            //判断该类上面是否有Component注解
            if(clz.isAnnotationPresent(Component.class)){
                //创建对象
                Object o = clz.newInstance();
                //获取该注解对象
                Component annotation = (Component)clz.getAnnotation(Component.class);
                //将注解中value属性的值作为key 对象作为值存入map中
                map.put(annotation.value(),o);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String id){
        return map.get(id);
    }
}
